package cn.zgwj.order.service.impl;

import cn.zgwj.basic.service.impl.BaseServiceImpl;
import cn.zgwj.basic.util.CodeGenerateUtils;
import cn.zgwj.basic.util.PageList;
import cn.zgwj.order.domain.AdoptOrder;
import cn.zgwj.order.domain.OrderAddress;
import cn.zgwj.order.mapper.AdoptOrderMapper;
import cn.zgwj.order.mapper.OrderAddressMapper;
import cn.zgwj.order.query.AdoptOrderQuery;
import cn.zgwj.order.service.IAdoptOrderService;
import cn.zgwj.org.domain.Employee;
import cn.zgwj.org.mapper.EmployeeMapper;
import cn.zgwj.pay.constants.PayConstants;
import cn.zgwj.pay.domain.PayBill;
import cn.zgwj.pay.mapper.PayBillMapper;
import cn.zgwj.pay.service.IPayBillService;
import cn.zgwj.pet.domain.Pet;
import cn.zgwj.pet.mapper.PetMapper;
import cn.zgwj.user.domain.Logininfo;
import cn.zgwj.user.domain.User;
import cn.zgwj.user.domain.UserAddress;
import cn.zgwj.user.mapper.UserAddressMapper;
import cn.zgwj.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private IPayBillService payBillService;


    @Override
    @Transactional
    public String submit(Map<String, Object> params, Logininfo currentLogininfo) {
        Long serviceMethod = Long.valueOf(params.get("service_method").toString());
        Long addressId = Long.valueOf(params.get("address_id").toString());
        Long payMethod = Long.valueOf(params.get("pay_method").toString());// // 0 支付宝 1 微信  2 银联
        Long petId = Long.valueOf(params.get("pet_id").toString());

        Pet pet = petMapper.loadById(petId);
        //1.下架宠物
        pet.setState(0);
        pet.setOffsaletime(new Date());
        //2.绑定宠物领养人
        User user = userMapper.loadByLogininfoId(currentLogininfo.getId());
        pet.setUser_id(user.getId());
        petMapper.update(pet);

        //3.生成领养订单   //3.1保存订单  //3.2订单地址
        // alt + shift + m  抽取代码成方法
        AdoptOrder order = initAdoptOrder(pet, user);
        String paySn = CodeGenerateUtils.generateUnionPaySn();
        //由于后面生成支付单,需要统一支付标识,所以我提前在这里生成
        order.setPaySn(paySn);
        adoptOrderMapper.save(order);//返回自增ID

        UserAddress address = userAddressMapper.loadById(addressId);
        OrderAddress orderAddress = initOrderAddress(order, address);
        orderAddressMapper.save(orderAddress);

        //生成支付单并支付
        PayBill bill = initPayBill(payMethod, user, order);
        payBillMapper.save(bill);
        //@TODO 支付超时定时器
        //封装统一的支付接口
        return payBillService.pay(bill);

    }

    private PayBill initPayBill(Long payMethod, User user, AdoptOrder order) {
        PayBill bill = new PayBill();
        bill.setDigest("[摘要]对领养"+order.getOrderSn()+"支付订单！");
        bill.setMoney(order.getPrice());
        bill.setUnionPaySn(order.getPaySn());
        bill.setLastPayTime(order.getLastPayTime());
        bill.setPayChannel(payMethod); // 0 支付宝 1 微信  2 银联
        bill.setBusinessType(PayConstants.LINGYANG_ORDER);
        bill.setBusinessKey(order.getId());
        bill.setUser_id(order.getUser_id());
        bill.setShop_id(order.getShop_id());
        bill.setNickName(user.getUsername());//冗余
        return bill;
    }

    private OrderAddress initOrderAddress(AdoptOrder order, UserAddress address) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(address, orderAddress);
        orderAddress.setOrder_id(order.getId());
        orderAddress.setOrderSn(order.getOrderSn());
        return orderAddress;
    }

    private AdoptOrder initAdoptOrder(Pet pet, User user) {
        AdoptOrder order = new AdoptOrder();
        order.setDigest("[摘要]对"+pet.getName()+"领养订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getSaleprice());//售价
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(user.getId());
        order.setShop_id(pet.getShop_id());
        //最后支付时间
        order.setLastPayTime(new Date(System.currentTimeMillis() + 15*60*1000));
        return order;
    }

    @Override
    public PageList<AdoptOrder> admin(AdoptOrderQuery query, Logininfo currentLogininfo) {
        Employee employee = employeeMapper.loadByLogininfoId(currentLogininfo.getId());
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<AdoptOrder> user(AdoptOrderQuery query, Logininfo currentLogininfo) {
        User user = userMapper.loadByLogininfoId(currentLogininfo.getId());
        query.setUserId(user.getId());
        return super.queryPage(query);
    }

}
